import 'package:flutter/material.dart';
import 'package:flutter_vant_kit/main.dart';
import 'package:pax_info_app/model/sys_info.dart';
import 'package:pax_info_app/repository/app_repository.dart';
import 'package:pax_info_app/ui/pages/login_page.dart';
import 'package:pax_info_app/ui/pages/setting/auto_clock.dart';
import 'package:pax_info_app/ui/pages/setting/clock_remind.dart';
import 'package:pax_info_app/ui/pages/webview/webview_page.dart';
import 'package:pax_info_app/utils/uidata.dart';
import 'package:package_info_plus/package_info_plus.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({Key? key}) : super(key: key);

  @override
  _SettingPageState createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {

  late DateTime _selectedDay;
  late DateTime _focusedDay;
  late bool _needPhoto;
  String _version = '';

  @override
  void initState() {
    _needPhoto = SysInfo.instance.needPhoto??true;
    _selectedDay = DateTime.now();
    _focusedDay = _selectedDay;
    _getVersion();
    super.initState();
  }

  _getVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();

    setState(() {
      _version = packageInfo.version;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: UIData.windowBg,
      appBar: AppBar(
        backgroundColor: const Color.fromRGBO(143, 148, 251, 1),
        title: const Text('设置'),
      ),
      body: SingleChildScrollView(
        child:
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const SizedBox(height: 10.0,),
            CellGroup(
              border: false,
              children: [
                Cell(
                  size: 'large',
                  title: '我的信息',
                  isLink: true,
                ),
              ],
            ),

            const SizedBox(height: 10.0,),
            CellGroup(
              border: false,
              children: [
                Cell(
                  size: 'large',
                  title: '假期管理',
                  isLink: true,
                  onClick: () {
                    Navigator.push(
                        context, MaterialPageRoute(
                        builder: (context) => WebViewPage(
                            url: 'https://info.paxsz.com/PersonOffice/Clock/Holidy/HolidayList.aspx')));
                  },
                ),
                Cell(
                  size: 'large',
                  title: '加班记录',
                  isLink: true,
                  onClick: () {
                    Navigator.push(
                        context, MaterialPageRoute(
                        builder: (context) => WebViewPage(
                            url: 'https://info.paxsz.com/PersonOffice/Clock/OverTime/ApplyList.aspx')));
                  },
                ),
                Cell(
                  size: 'large',
                  title: '出差管理',
                  isLink: true,
                  onClick: () {
                    Navigator.push(
                        context, MaterialPageRoute(
                        builder: (context) => WebViewPage(
                            url: 'https://info.paxsz.com/PersonOffice/Clock/EvectionManage/EvectionList.aspx')));
                  },
                ),
              ],
            ),

            const SizedBox(height: 10.0,),
            CellGroup(
              border: false,
              children: [
                Cell(
                  size: 'large',
                  title: '打卡提醒',
                  isLink: true,
                  onClick: () {
                    Navigator.push(
                        context, MaterialPageRoute(builder: (context) => ClockRemind()));
                  },
                ),
                Cell(
                  size: 'large',
                  title: '打卡是否需要照片',
                  customRight: SizedBox(
                    height: 20.0,
                    child: Switch(
                      value: _needPhoto,
                      activeColor: UIData.primaryColor,
                      onChanged: (val) {
                        _needPhoto = val;
                        SysInfo.instance.needPhoto = _needPhoto;
                        SysInfo.instance.save();
                        setState(() {});
                      },
                    ),
                  )
                ),
                Cell(
                  size: 'large',
                  title: '自动打卡',
                  isLink: true,
                  onClick: () {
                    Navigator.push(
                        context, MaterialPageRoute(builder: (context) => AutoClock()));
                  },
                ),
              ],
            ),

            const SizedBox(height: 10.0,),
            CellGroup(
              border: false,
              children: [
                Cell(
                  size: 'large',
                  title: '帮助与反馈',
                  isLink: true,
                ),
                Cell(
                  size: 'large',
                  title: '关于PAX INFO',
                  value: '版本$_version',
                  isLink: true,
                ),
              ],
            ),

           const SizedBox(height: 10.0,),
           CellGroup(
             border: false,
             children: [
               NButton(
                 block: true,
                 text: '退出登录',
                 size: 'large',
                  textColor: UIData.textNormal,
                  color: UIData.white,
                 onClick: () {
                   AppRepository.logout();
                   Navigator.pushReplacement(
                       context, MaterialPageRoute(builder: (context) => LoginPage()));
                 },
               ),
             ],
           ),
          ],
        ),
      ),
    );
  }
}